001 /* 002 * Copyright 2005-2006 Stephen McConnell. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 013 * implied. 014 * 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package net.dpml.depot; 020 021 import java.io.OutputStream; 022 import java.io.IOException; 023 import java.io.ObjectOutputStream; 024 import java.io.BufferedOutputStream; 025 import java.net.Socket; 026 import java.net.SocketException; 027 import java.util.logging.Handler; 028 import java.util.logging.LogRecord; 029 030 import net.dpml.lang.PID; 031 032 /** 033 * Logging message handler that redirects log messages from a subprocess to 034 * a remote logging system. 035 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a> 036 * @version 1.0.1 037 */ 038 public class DepotHandler extends Handler 039 { 040 private static final PID ID = new PID(); 041 042 private final Socket m_socket; 043 private final OutputStream m_output; 044 private final ObjectOutputStream m_stream; 045 046 /** 047 * Creation of a new handler instance. 048 * @exception Exception if an error occurs 049 */ 050 public DepotHandler() throws Exception 051 { 052 this( "localhost", 2020 ); 053 } 054 055 /** 056 * Creation of a new handler instance. 057 * @param host the log server host 058 * @param port the port 059 * @exception IOException if an IO error occurs 060 */ 061 public DepotHandler( String host, int port ) throws IOException 062 { 063 m_socket = new Socket( host, port ); 064 m_output = m_socket.getOutputStream(); 065 m_stream = new ObjectOutputStream( new BufferedOutputStream( m_output ) ); 066 } 067 068 /** 069 * Flush the handler. 070 */ 071 public void flush() 072 { 073 } 074 075 /** 076 * Close the log record handler. 077 */ 078 public void close() 079 { 080 try 081 { 082 m_output.close(); 083 } 084 catch( Exception e ) 085 { 086 e.printStackTrace(); 087 } 088 try 089 { 090 m_socket.close(); 091 } 092 catch( Exception e ) 093 { 094 e.printStackTrace(); 095 } 096 } 097 098 /** 099 * Publish a log record. The implementation packages the supplied log 100 * record under a LogStatement, binding the record with the associated process 101 * identifier. The statement is subsequently writen to a socket established 102 * within the instance constructor. 103 * 104 * @param record the log record to publish 105 */ 106 public void publish( LogRecord record ) 107 { 108 synchronized( m_socket ) 109 { 110 if( m_socket.isClosed() ) 111 { 112 return; 113 } 114 115 try 116 { 117 LogStatement statement = new LogStatement( ID, record ); 118 m_stream.writeObject( statement ); 119 m_stream.flush(); 120 } 121 catch( SocketException e ) 122 { 123 e.printStackTrace(); 124 } 125 catch( Exception e ) 126 { 127 e.printStackTrace(); 128 } 129 } 130 } 131 } 132